import { Component, Vue } from 'vue-property-decorator'
import { Message } from 'element-ui'
import { NetworkErrorMap } from '@/config/network'

export function throwErrorMessageWhenCatch(code = '999'): void {
  if (this.$message) {
    this.$message.error({
      content: NetworkErrorMap[code] || '操作失败，请稍后再试',
      duration: 2,
    })
  } else if (Message) {
    Message({
      message: NetworkErrorMap[code] || '操作失败，请稍后再试',
      type: 'error',
      duration: 2 * 1000,
    })
  }
}

export function throwErrorMessage(msg?: string): void {
  if (this.$message) {
    this.$message.error({
      content: msg || '操作失败，请稍后再试',
      duration: 2,
    })
  } else if (Message) {
    Message({
      message: msg || '操作失败，请稍后再试',
      type: 'error',
      duration: 2 * 1000,
    })
  }
}

export function throwSuccessMessage(msg?: string): void {
  if (this.$message) {
    this.$message.success({
      content: msg || '操作成功',
      duration: 2,
    })
  } else if (Message) {
    Message({
      message: msg || '操作成功',
      type: 'success',
      duration: 2 * 1000,
    })
  }
}

@Component({
  name: 'DefaultMixin',
})
export default class extends Vue {
  public refreshPage(): void {
    // 手动重定向页面到 '/redirect' 页面
    const { fullPath } = this.$route
    this.$router.replace({
      path: '/redirect?fullPath=' + fullPath,
    })
  }
  public throwErrorMessage = throwErrorMessage

  public throwSuccessMessage = throwSuccessMessage

  public throwErrorMessageWhenCatch = throwErrorMessageWhenCatch
}
